// 联盟活动
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.activity;
	var db_id_prefix = config.dbIdPrefix.activity;
	var BusCon = require("../bus/_con");
	var helper = require("../_include/helper");

	//构造函数
	var _con = function (activity_name,info){
		var self = this;
		//ID编号
		self._id = league_name||(db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16));
		//校验活动属性
		//活动名称
		if (!helper.folder_name(league_name)) {
			throwCusError("活动名命名不规范")
		}
		self.league_name = league_name;
		//活动介绍
		self.intro = info.intro?String(info.intro):"";
		if (self.intro.length>config.leagueDescriptionLength) {
			throwCusError("活动简介长度超出长度限制");
		}
		//活动背景图，HTML
		self.bg_html = self.bg_html;

		//活动形式
		/*{
			//整场满减
			type:"满减",
			data:{
				//满399减100，可以设置多个字段
				390:100
			},
			goods:[G_id,..]
		},{
			type:"折扣",
			data:{
				// "all":8.8//0.1~9.9折
				//特殊商品自定义折扣价
				"G_id":9.0,
				...
			}
		}*/
		// //这些活动届时会开启
		// self.format = self.format||[];

		//活动BUFF
		self.buffs = [];
		//活动时间
		self.begin_time = +self.begin_time;
		self.end_time = +self.end_time;
		if (!self.begin_time) {
			throwCusError("开始时间不可为空")
		}
		if (!self.end_time) {
			throwCusError("结束时间不可为空")
		}
		if (self.begin_time<self.end_time) {
			throwCusError("开始时间与结束时间出错")
		}
		//创建时间
		self.create_time = +new Date;
		//状态：未开始、举行中、举行完毕、冻结（被举报并查出有问题）
		self.status = "未开始";

		console.log("创建了一个活动：",league_name);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
}());
